package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/18 10:23
 */
@Controller
public class RegisterController {


    @Autowired(required = false)
    private UserMapper userMapper;


    @GetMapping("register")
    public String register() {
        return "register";
    }

    @PostMapping("check")
    public String check(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) {
        String name = request.getParameter("name");
        String phonenumber = request.getParameter("phonenumber");
        String password = request.getParameter("password");
        User user = new User(name, password, Integer.parseInt(phonenumber));
        User userbyname = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        if (userbyname != null) {
            //用户名已注册
        } else {
            //用户注册
            userMapper.insert(user);
            session.setAttribute("user", user);
        }

        return "redirect:/index";
    }


}
